

package com.maintain.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;


import com.ifeiyang.bijia.R;


public class BorderCircleImage extends ImageView {


	private Context mContext;


	private int mw;


	private int mh;


	private int a;




	private int r;


	private int bDefaultColor = 0xFFFFFFFF;


	private int bThickness;


	private int bOutColor;


	private int bInColor;


	public BorderCircleImage(Context context, AttributeSet attrs) {


		super(context, attrs);
		mContext = context;
		setCustomAttributes(attrs);
	}


	private void setCustomAttributes(AttributeSet attrs) {


		TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);
		bThickness = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_thickness, 0);
		bOutColor = a.getColor(R.styleable.RoundedImageView_border_outside_color, bDefaultColor);
		bInColor = a.getColor(R.styleable.RoundedImageView_border_inside_color, bDefaultColor);
		bThickness = 2;
		a.recycle();
	}


	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {


		mw = w;
		mh = h;
		a = w < h ? w : h;
		r = a / 2;
	}


	@Override
	protected void onDraw(Canvas canvas) {


		Bitmap b = ((BitmapDrawable) getDrawable()).getBitmap();
		if (b == null) {
			return;
		}
		drawBorder(canvas);
		drawBitmap(canvas, b);
	}


	private void drawBitmap(Canvas canvas, Bitmap b) {


		Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
		Bitmap circleImage = getCircleImage(bitmap);
		canvas.drawBitmap(circleImage, 0, 0, null);
	}


	private void drawBorder(Canvas canvas) {


		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(bThickness);


		paint.setColor(bOutColor);

		canvas.drawCircle(r, r, r - bThickness / 2, paint);

		paint.setColor(bInColor);

		canvas.drawCircle(r, r, r - bThickness * 3 / 2, paint);
	}


	private Bitmap getCircleImage(Bitmap bitmap) {


		Bitmap output = Bitmap.createBitmap(a, a, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		canvas.drawARGB(0, 0, 0, 0);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);

		bitmap = getCenterBitmap(bitmap);
		bitmap = Bitmap.createScaledBitmap(bitmap, a - bThickness * 2, a - bThickness * 2, true);

		canvas.drawCircle(r, r, r - bThickness * 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, 0 + bThickness * 2, 0 + bThickness * 2, paint);
		return output;

	}


	private Bitmap getCenterBitmap(Bitmap bitmap) {


		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		int abs = Math.abs((width - height) / 2);
		if (width > height) {
			return Bitmap.createBitmap(bitmap, abs, 0, height, height);
		} else if (width < height) {
			return Bitmap.createBitmap(bitmap, 0, abs, width, width);
		} else {
			return bitmap;
		}

	}



}
